//
//  InPutTableViewCell.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/12.
//

#import "InPutTableViewCell.h"

@implementation InPutTableViewCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        self.bottomImgView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.bottomImgView];
        self.bottomImgView.userInteractionEnabled = YES;
        [self.bottomImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.bottom.equalTo(self.contentView.mas_bottom);
            make.top.equalTo(self.contentView.mas_top).offset(37);
        }];
        
        self.topImgView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.topImgView];
        self.topImgView.userInteractionEnabled = YES;
        [self.topImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(72);
            make.right.equalTo(self.contentView.mas_right).offset(-72);
            make.height.equalTo(@42);
            make.top.equalTo(self.contentView.mas_top).offset(15);
        }];
        
        self.titleLab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:14] color:[UIColor colorWithHexString:@"#FFEFC0"]];
        self.titleLab.textAlignment = 1;
        [self.topImgView addSubview:self.titleLab];
        self.titleLab.adjustsFontSizeToFitWidth = YES;
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.topImgView.mas_left);
            make.right.equalTo(self.topImgView.mas_right);
            make.top.equalTo(self.topImgView.mas_top);
            make.bottom.equalTo(self.topImgView.mas_bottom);
        }];
        
        self.textFiled = [[CreditTextFiled alloc] init];
        self.textFiled.textColor = [UIColor colorWithHexString:@"#022D06"];
        self.textFiled.font = [UIFont fontWithName:kFuturaMedium size:14];
        self.textFiled.textAlignment = 1;
        self.textFiled.delegate = self;
        [self.bottomImgView addSubview:self.textFiled];
        [self.textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bottomImgView.mas_left).offset(30);
            make.right.equalTo(self.bottomImgView.mas_right).offset(-30);
            make.bottom.equalTo(self.bottomImgView.mas_bottom);
            make.top.equalTo(self.titleLab.mas_bottom);
        }];
       
        [self.textFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        self.arrowImgView = [[UIImageView alloc] init];
        [self.bottomImgView addSubview:self.arrowImgView];
        [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@16);
            make.height.equalTo(@14);
            make.right.equalTo(self.bottomImgView.mas_right).offset(-12);
            make.centerY.equalTo(self.textFiled.mas_centerY);
        }];
        
        self.iconV = [[UIImageView alloc] init];
        [self.bottomImgView addSubview:self.iconV];
        self.iconV.userInteractionEnabled = YES;
        [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bottomImgView.mas_left);
            make.width.equalTo(@0);
            make.height.equalTo(@0);
            make.centerY.equalTo(self.textFiled.mas_centerY);
        }];
        self.iconV.hidden = YES;
        
    }
    return self;
}

- (void)setModel:(InPutModel *)model{
    _model = model;
    
    
    
    self.topImgView.image = [UIImage imageNamed:@"icon_per2"];
    self.bottomImgView.image = [UIImage imageNamed:@"icon_per3"];
    
    self.titleLab.text = esString(model.ego);
    self.textFiled.placeholder = esString(model.raw);
    
    self.textFiled.text  = esString(model.mightn);
    if (self.isBank) {
        self.textFiled.text  = esString(model.patterns);
    }
    
    if (!PMStringIsEmpty(model.raw)) {
        NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:self.textFiled.placeholder attributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#638473"]}];
        self.textFiled.attributedPlaceholder = placeholderString;
    }
    
    if ([model.sensitiveness integerValue] == 1) {
        self.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    } else {
        self.textFiled.keyboardType = UIKeyboardTypeDefault;
    }
    
    self.arrowImgView.image = [UIImage imageNamed:@"icon_idf12"];
    if ([model.weak isEqualToString:kMakemoneyk] || [model.weak isEqualToString:kMakemoneym]) {
        self.textFiled.userInteractionEnabled = NO;
        self.arrowImgView.hidden = NO;
    } else {
        self.textFiled.userInteractionEnabled = YES;
        self.arrowImgView.hidden = YES;
    }
    
    
    if (!PMStringIsEmpty(model.spoon)) {
        self.iconV.hidden = NO;
        
        CGRect rec = [esString(self.textFiled.text) boundingRectWithSize:CGSizeMake(kScreenWidth - 30 - 60, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:kFuturaMedium size:14]} context:nil];
        
        [self.iconV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@((kScreenWidth - 30)/2 - rec.size.width/2 - 30));
            make.width.equalTo(@25);
            make.height.equalTo(@25);
            make.centerY.equalTo(self.textFiled.mas_centerY);
        }];
        
    }
    else {
        self.iconV.hidden = YES;
        [self.iconV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bottomImgView.mas_left);
            make.width.equalTo(@0);
            make.height.equalTo(@0);
            make.centerY.equalTo(self.textFiled.mas_centerY);
        }];
    }
    
    [self.iconV sd_setImageWithURL:[NSURL URLWithString:esString(model.spoon)]];
    
    
}

- (void)textFieldDidChange:(UITextField *) textField{
    NSString *textString = esString(textField.text);
   
    self.model.mightn = textString;
    if (self.isBank) {
        self.model.patterns  = textString;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@implementation InPut2TableViewCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        
        self.backV = [[UIView alloc] init];
        [self.contentView addSubview:self.backV];
        self.backV.backgroundColor = [UIColor whiteColor];
        [self.backV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.height.equalTo(@100);
            make.top.equalTo(self.contentView.mas_top);
        }];
        self.backV.layer.cornerRadius = 12;
        self.backV.layer.masksToBounds = YES;
        
        self.titleLab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:14] color:[UIColor colorWithHexString:@"#022D06"]];
        [self.backV addSubview:self.titleLab];
        self.titleLab.adjustsFontSizeToFitWidth = YES;
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backV.mas_left).offset(20);
            make.right.equalTo(self.backV.mas_right).offset(-10);
            make.top.equalTo(self.backV.mas_top);
            make.height.equalTo(@42);
        }];
        
        self.iconView = [[UIImageView alloc] init];
        [self.backV addSubview:self.iconView];
        [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backV.mas_left).offset(15);
            make.right.equalTo(self.backV.mas_right).offset(-15);
            make.top.equalTo(self.titleLab.mas_bottom);
            make.height.equalTo(@46);
        }];
        
        self.textFiled = [[CreditTextFiled alloc] init];
        self.textFiled.textColor = [UIColor colorWithHexString:@"#022D06"];
        self.textFiled.font = [UIFont fontWithName:kFuturaMedium size:14];
        self.textFiled.textAlignment = 0;
        self.textFiled.delegate = self;
        [self.backV addSubview:self.textFiled];
        [self.textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backV.mas_left).offset(30);
            make.right.equalTo(self.backV.mas_right).offset(-30);
            make.height.equalTo(@46);
            make.top.equalTo(self.titleLab.mas_bottom);
        }];
       
        [self.textFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        self.arrowImgView = [[UIImageView alloc] init];
        [self.backV addSubview:self.arrowImgView];
        [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@16);
            make.height.equalTo(@14);
            make.right.equalTo(self.backV.mas_right).offset(-12);
            make.centerY.equalTo(self.textFiled.mas_centerY);
        }];
        
        
    }
    return self;
}

- (void)setModel:(InPutModel *)model{
    _model = model;
    
    
    
    self.iconView.image = [UIImage imageNamed:@"icon_bk3"];
   
    self.titleLab.text = esString(model.ego);
    self.textFiled.placeholder = esString(model.raw);
    
    self.textFiled.text  = esString(model.mightn);
    if (self.isBank) {
        self.textFiled.text  = esString(model.patterns);
    }
    
    if (!PMStringIsEmpty(model.raw)) {
        NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:self.textFiled.placeholder attributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#638473"]}];
        self.textFiled.attributedPlaceholder = placeholderString;
    }
    
    if ([model.sensitiveness integerValue] == 1) {
        self.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    } else {
        self.textFiled.keyboardType = UIKeyboardTypeDefault;
    }
    
    self.arrowImgView.image = [UIImage imageNamed:@"icon_idf12"];
    if ([model.weak isEqualToString:kMakemoneyk] || [model.weak isEqualToString:kMakemoneym]) {
        self.textFiled.userInteractionEnabled = NO;
        self.arrowImgView.hidden = NO;
    } else {
        self.textFiled.userInteractionEnabled = YES;
        self.arrowImgView.hidden = YES;
    }
}

- (void)textFieldDidChange:(UITextField *) textField{
    NSString *textString = esString(textField.text);
   
    self.model.mightn = textString;
    if (self.isBank) {
        self.model.patterns = textString;
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
